var dtGridColumns = [
    {
        id: 'delFlag',
        type: 'int',
        hideQuery:true,
        'export':false,
        hideQueryType:'eq',
        hideQueryValue:1,
        hide:true

    },
    {
        id: 'title',
        title: '标题',
        type: 'string',
        headerClass: 'text-left width-200',
        columnClass: 'text-left width-200',
        fastQuery: true,
        fastQueryType: 'lk',
        fastSort: false,
        hideType: 'sm|xs'
    },
    {
        id: 'site.name',
        type: 'string',
        title: '栏目',
        headerClass: 'text-left width-100',
        columnClass: 'text-left width-100',
        fastQuery: true,
        fastQueryType: 'lk',
        fastSort: false,
    },
    {
        id: 'author',
        type: 'string',
        title: '作者',
        headerClass: 'text-left width-100',
        columnClass: 'text-left width-100',
        fastQuery: true,
        fastSort: false,
        fastQueryType: 'lk'
    },
    {
        id: 'sortNo',
        title: '排序',
        type: 'string',
        headerClass: 'text-left width-50',
        columnClass: 'text-left width-50',
        fastQuery: false,
        fastQueryType: 'eq',
        fastSort: false,
        hideType: 'sm|xs'
    },
    {
        id: 'publishTime',
        title: '发布时间',
        type: 'date',
        format: 'yyyy-MM-dd HH:mm:ss',
        headerClass: 'text-center width-200',
        columnClass: 'text-center width-200',
        fastSort: false,
        fastQuery: true,
        fastQueryType: 'range'
    },
    {
        id: 'status',
        title: '状态',
        type: 'string',
        columnClass: 'text-center width-50',
        fastSort: false,
        fastQuery: true,
        fastQueryType: 'eq',
        hideType: 'md|sm|xs',
        codeTable: {
            1: $lang.articleStatus.WAIT,
            2: $lang.articleStatus.PUBLISHED,
            3: $lang.articleStatus.BACK
        }
    },
    {
        id: 'operation',
        title: '管理操作',
        type: 'string',
        'export':false,
        columnClass: 'text-center width-200',
        fastSort: false,
        extra: false,
        resolution: function (value, record, column, grid, dataNo, columnNo) {
            var html = '', edit, view, publish, back, del;
            edit = "<a data-target='#editModal' class='btn btn-sm btn-primary m-r-10' data-toggle='modal' data-no='" + dataNo + "' ><i class='fa fa-edit'></i>&nbsp;编辑&nbsp;</a>";
            publish = "<a href='javascript:;' class='btn btn-danger btn-sm m-r-10' onclick='information.pubInfo(" + record.id + ",\"" + record.title + "\")'><i class='fa fa-paper-plane-o'></i>&nbsp;发布&nbsp;</a>";
            back = "<a href='javascript:;' class='btn btn-danger btn-sm m-r-10' onclick='information.backInfo(" + record.id + ",\"" + record.title + "\")'><i class='fa fa-reply-all'></i>&nbsp;撤回&nbsp;</a>";
            del = "<a href='javascript:;' class='btn btn-danger btn-sm m-r-10' onclick='information.delInfo(" + record.id + ",\"" + record.title + "\")'><i class='fa fa-trash-o'></i>&nbsp;删除&nbsp;</a>";
            view = "<a data-target='#lookModal' class='btn btn-sm btn-primary m-r-10' data-toggle='modal' data-no='" + dataNo + "' ><i class='fa fa-eye Example of eye'></i>&nbsp;查看&nbsp;</a>";
            if (record.status != 2) {
                html = edit + publish + del+view;
            } else if (record.status == 2) {
                html = back+view;
            }

            return html;
        }
    }
];

var dtGridOption = {
    lang: 'zh-cn',
    ajaxLoad: true,
    loadURL: ncGlobal.adminRoot + 'article/json/list',
    exportURL: ncGlobal.adminRoot + 'article/json/export',
    exportFileName: '内容列表',
    columns: dtGridColumns,
    gridContainer: 'dtGridContainer',
    toolbarContainer: 'dtGridToolBarContainer',
    tools: 'refresh|faseQuery|export[excel]',
    pageSize: 10,
    pageSizeLimit: [10, 20, 50],
    ncColumnsType: {int: ["id", "sortNo", "delFlag"], Timestamp: ["createTime", "publishTime"]},
    onGridComplete: function (grid) {
    }
};

var grid = $.fn.DtGrid.init(dtGridOption);
grid.sortParameter.columnId = ['asc_site.sortNo','asc_t.sortNo','desc_t.createTime'];
// 默认查询条件
grid.fastQueryParameters = new Object();
grid.fastQueryParameters['eq_delFlag'] = 1;

var dtGridColumnsRelative = [
    {
        id: 'id',
        title: '编号',
        type: 'number',
        columnClass: 'text-center width-100',
        fastSort: true

    },
    {
        id: 'name',
        title: '栏目名称',
        type: 'string',
        columnClass: 'text-center',
        fastQuery: true,
        fastQueryType: 'lk',
    },
    {
        id: 'level',
        title: '栏目层级',
        type: 'number',
        columnClass: 'text-center'
    },
    {
        id: 'pname',
        title: '上级栏目',
        type: 'string',
        columnClass: 'text-center',
        fastSort: false,
        hideType: 'md|sm|xs',
        resolution: function (value, record, column, grid, dataNo, columnNo) {
            return value != '' ? value : "--";
        },
    }
];

var dtGridOptionSite = {
    lang: 'zh-cn',
    ajaxLoad: true,
    loadURL: ncGlobal.adminRoot + '/site/json/list',
    exportFileName: '栏目列表',
    columns: dtGridColumnsRelative,
    gridContainer: 'dtGridContainerSite',
    toolbarContainer: 'dtGridToolBarContainerSite',
    pageSize: 10,
    pageSizeLimit: [10, 20, 50],
    ncColumnsType: {int: ["id"], Timestamp: ["createTime"]},
    onRowDblClick: function (value, record, column, grid, dataNo, columnNo, cell, row, extraCell, e) {
        if (record.name != null) {
            $("#chooseSiteEdit").text(record.name);
            $("#chooseSiteAdd").text(record.name);
        }
        else {
            $("#chooseSiteEdit").text("");
            $("#chooseSiteAdd").text("");
        }
        $('input[name="siteId"]').val(record.id);
        $("#siteModal").hide();//隐藏关联栏目窗口
        return false;
    }
};

var grid1 = $.fn.DtGrid.init(dtGridOptionSite);
grid1.fastQueryParameters = new Object();
grid1.fastQueryParameters['eq_delFlag'] = 1;
//排序
grid1.sortParameter.columnId = ['asc_id'];

var information = function () {

    /**
     * 图片缩放
     */
    function _refreshImage() {
        $(".viewImage").jqthumb({
            width: 160,
            height: 100,
            after: function (imgObj) {
                imgObj.css('opacity', 0).animate({opacity: 1}, 1500);
            }
        });
    }
    /**
     * 图片缩放
     */
    function _refreshImage2() {
        $(".viewImage").jqthumb({
            width: 250,
            height: 120,
            position: { top: '100%', left: '100%' },
            after: function (imgObj) {
                imgObj.css('opacity', 0).animate({opacity: 1}, 1500);
            }
        });
    }


    //编辑器对象[添加]
    var _ueAdd;
    //编辑器对象[编辑]
    var _ueEdit;
    //查看器对象[查看]
    var _ueLook;

    /**
     * 删除
     */
    function _delInfo(id, content) {
        var tpl = '您选择对内容 <strong>' + content + '</strong> 进行删除操作，删除后将无法恢复。<br/>您确定要进行删除操作吗?'
        $.ncConfirm({
            url: ncGlobal.adminRoot + "article/json/del",
            data: {
                id: id
            },
            content: tpl,
            alertTitle: "删除操作"
        });
    }

    /**
     * 撤回
     */
    function _backInfo(id, content) {
        var tpl = '您是否确定撤回内容 <strong>' + content + '</strong> ?'
        $.ncConfirm({
            url: ncGlobal.adminRoot + "article/json/publish",
            data: {
                id: id
            },
            content: tpl,
            alertTitle: "撤回操作"
        });
    }

    /**
     * 发布
     */
    function _pubInfo(id, content) {
        var tpl = '您是否确定发布内容 <strong>' + content + '</strong> ?'
        $.ncConfirm({
            url: ncGlobal.adminRoot + "article/json/publish",
            data: {
                id: id
            },
            content: tpl,
            alertTitle: "发布操作"
        });
    }

    var addInfo = {
        initAddModal: function () {
            $addForm = $("#addForm");
            $addForm.find('input[name="title"]').val("");
            $addForm.find('input[name="sortNo"]').val("");
            $addForm.find('input[name="author"]').val("");
            $addForm.find('input[name="source"]').val("");
            $addForm.find('input[name="summary"]').val("");
            $('#addFormPicPath').val("");
            $('#addFormPicImg').attr('src', ncGlobal.imgRoot + "default_image.gif");
            $('#chooseSiteAdd').text("");
            $('#addModal').find('div[name="preview"]').html('');
            information.ueAdd.setContent("");
            $(".alert-danger").remove();
            $addForm.psly().reset();
        }
    }

    /**
     * 事件绑定
     * @private
     */
    function _bindEvent() {
        //模糊搜索
        $('#customSearch').click(function () {
            grid.fastQueryParameters['lk_title'] = $('#keyword').val();
            grid.pager.startRecord = 0;
            grid.pager.nowPage = 1;
            grid.pager.recordCount = -1;
            grid.pager.pageCount = -1;
            grid.refresh(true);
        });
        //图片上传
        $("#addFormPic").fileupload({
            dataType: 'json',
            url: ncGlobal.adminRoot + "file/upload",
            send: function (e, data) {
                //进行图片格式验证
                var reg = /^image\/(gif|jpg|jpeg|png|GIF|JPG|PNG|JPEG)$/;
                if (reg.test(data.files[0].type)) {
                    if (data.files[0].size > 2 * 1024 * 1024) {
                        $.ncAlert({
                            closeButtonText: "关闭",
                            autoCloseTime: 3,
                            content: "图片格式超过2M请重新上传！"
                        });
                        return false;
                    }
                } else {
                    $.ncAlert({
                        closeButtonText: "关闭",
                        autoCloseTime: 3,
                        content: "请上传符合格式要求的图片！"
                    });
                    return false;
                }
            },
            done: function (e, data) {
                if (data.result.code == 200) {
                    $('#addFormPicPath').val(data.result.data);
                    $('#addFormPicImg').attr('src', ncGlobal.fileRoot + data.result.data);
                    //图片同比例缩放-新增
                    _refreshImage();
                } else {
                    $.ncAlert({
                        closeButtonText: "关闭",
                        autoCloseTime: 3,
                        content: data.result.message
                    })
                }
            }
        });
        /**
         * 上传插件绑定
         */
        $("#editFormPic").fileupload({
            dataType: 'json',
            url: ncGlobal.adminRoot + "file/upload",
            send: function (e, data) {
                //进行图片格式验证
                var reg = /^image\/(gif|jpg|jpeg|png|GIF|JPG|PNG|JPEG)$/;
                if (reg.test(data.files[0].type)) {
                    if (data.files[0].size > 2 * 1024 * 1024) {
                        $.ncAlert({
                            closeButtonText: "关闭",
                            autoCloseTime: 3,
                            content: "图片格式超过2M请重新上传！"
                        });
                        return false;
                    }
                } else {
                    $.ncAlert({
                        closeButtonText: "关闭",
                        autoCloseTime: 3,
                        content: "请上传符合格式要求的图片！"
                    });
                    return false;
                }
            },
            done: function (e, data) {
                if (data.result.code == 200) {
                    $('#editFormPicPath').val(data.result.data);
                    $('#editFormPicImg').attr('src', ncGlobal.fileRoot + data.result.data);
                    //图片同比例缩放-编辑
                    _refreshImage();
                } else {
                    $.ncAlert({
                        closeButtonText: "关闭",
                        autoCloseTime: 3,
                        content: data.result.message
                    })
                }
            }
        });
        // 新增资讯对话框
        $("#addForm").on("nc.formSubmit.success", function (e) {
            addInfo.initAddModal();
            _refreshImage();
        })
        //添加微信事件时显示时
        $('#addModal').on('show.bs.modal', function (event) {
            addInfo.initAddModal();
            _refreshImage();
        });
        // bycj [ 编辑对话框显示时调用 ]
        $('#editModal').on('show.bs.modal', function (event) {
            var    //获取接受事件的元素
                button = $(event.relatedTarget),
                //获取data 参数
                datano = button.data('no'),
                modal = $(this),
                //获取列表框中的原始数据
                gridData = grid.sortOriginalDatas[datano],
                editForm = $("#editForm");
            modal.find('input[name="id"]').val(gridData.id);
            modal.find('input[name="siteId"]').val(gridData.siteId);
            modal.find('input[name="title"]').val(gridData.title);
            modal.find('input[name="author"]').val(gridData.author);
            modal.find('input[name="source"]').val(gridData.source);
            modal.find('input[name="summary"]').val(gridData.summary);
            modal.find('input[name="sortNo"]').val(gridData.sortNo);
            $('#chooseSiteEdit').text(gridData.site.name);
            $('#editFormPicPath').val(gridData.litpic);
            $('#editFormPicImg').attr('src', ncGlobal.fileRoot + gridData.litpic);
            $('#editModal').find('div[name="preview"]').html('');
            _refreshImage();
            if (gridData.content != null) {
                information.ueEdit.setContent(gridData.content);
            }

            //清除错误提示
            editForm.psly().reset();
            $(".alert-danger").remove()
        });

        // 查看文章对话框]
        $('#lookModal').on('show.bs.modal', function (event) {
            var    //获取接受事件的元素
                button = $(event.relatedTarget),
                //获取data 参数
                datano = button.data('no'),
                modal = $(this),
                //获取列表框中的原始数据
                gridData = grid.sortOriginalDatas[datano];
            modal.find('input[name="site.name"]').val(gridData.site.name);
            modal.find('input[name="title"]').val(gridData.title);
            modal.find('input[name="author"]').val(gridData.author);
            modal.find('input[name="source"]').val(gridData.source);
            modal.find('input[name="summary"]').val(gridData.summary);
            modal.find('input[name="sortNo"]').val(gridData.sortNo);
            if(gridData.litpic!=undefined||gridData.litpic!=null){
                $('#lookFormPicImg').attr('src', ncGlobal.fileRoot + gridData.litpic);
            }else{
                $('#lookFormPicImg').attr('src', ncGlobal.imgRoot + "default_image.gif");
            }
            _refreshImage2();
            if (gridData.content != null) {
                information.ueLook.setContent(gridData.content);
            }else {
                information.ueLook.setContent("");
            }
            information.ueLook.setDisabled();

        });

        // 图片上传
        $("#addFormFile").fileupload({
            dataType: 'json',
            url: ncGlobal.adminRoot + "file/upload",
            send: function (e, data) {
                //进行图片格式验证
                var reg = /^image\/(gif|jpg|jpeg|png|GIF|JPG|PNG|JPEG)$/;
                if (reg.test(data.files[0].type)) {
                    if (data.files[0].size > 2 * 1024 * 1024) {
                        $.ncAlert({
                            closeButtonText: "关闭",
                            autoCloseTime: 3,
                            content: "图片格式超过2M请重新上传！"
                        });
                    }
                } else {
                    $.ncAlert({
                        closeButtonText: "关闭",
                        autoCloseTime: 3,
                        content: "请上传符合格式要求的图片！"
                    });
                    return;
                }
            },
            done: function (e, data) {
                if (data.result.code == 200) {
                    var html = '<span class="col-md-3 col-sm-6 m-b-10"><img class="viewImage" src="' + ncGlobal.fileRoot + data.result.data + '"><a name="insert" href="javascript:;" class="btn btn-success btn-xs m-t-5"><i class="fa fa-arrow-circle-o-up"></i>&nbsp;插入</a><a name="delete" href="javascript:;" class="btn btn-danger btn-xs m-t-5 m-l-5"><i class="fa fa-trash-o"></i>&nbsp;删除</a></span>';
                    $('#addModal').find('div[name="preview"]').append(html);
                    var item = $('#addModal').find('div[name="preview"] > span').last();
                    $(item).find('a[name="insert"]').on('click', function () {
                        information.ueAdd.execCommand('inserthtml', '<img src="' + ncGlobal.fileRoot + data.result.data + '">');
                    });
                    $(item).find('a[name="delete"]').on('click', function () {
                        $(this).parents('span').remove();
                    });
                    //图片同比例缩放-默认
                    $('.information-pic img').jqthumb({
                        width: 100,
                        height: 100,
                        after: function (imgObj) {
                            imgObj.css('opacity', 0).animate({opacity: 1}, 2000);
                        }
                    });
                    _refreshImage();
                } else {
                    $.ncAlert({
                        closeButtonText: "关闭",
                        autoCloseTime: 3,
                        content: data.result.message
                    })
                }
            }
        });
        // 图片上传
        $("#editFormFile").fileupload({
            dataType: 'json',
            url: ncGlobal.adminRoot + "file/upload",
            send: function (e, data) {
                //进行图片格式验证
                var reg = /^image\/(gif|jpg|jpeg|png|GIF|JPG|PNG|JPEG)$/;
                if (reg.test(data.files[0].type)) {
                    if (data.files[0].size > 2 * 1024 * 1024) {
                        $.ncAlert({
                            closeButtonText: "关闭",
                            autoCloseTime: 3,
                            content: "图片格式超过2M请重新上传！"
                        });
                    }
                } else {
                    $.ncAlert({
                        closeButtonText: "关闭",
                        autoCloseTime: 3,
                        content: "请上传符合格式要求的图片！"
                    });
                    return;
                }
            },
            done: function (e, data) {
                if (data.result.code == 200) {
                    var html = '<span class="col-md-3 col-sm-6 m-b-10"><img class="viewImage" src="' + ncGlobal.fileRoot + data.result.data + '"><a name="insert" href="javascript:;" class="btn btn-success btn-xs m-t-5"><i class="fa fa-arrow-circle-o-up"></i>&nbsp;插入</a><a name="delete" href="javascript:;" class="btn btn-danger btn-xs m-t-5 m-l-5"><i class="fa fa-trash-o"></i>&nbsp;删除</a></span>';
                    $('#editModal').find('div[name="preview"]').append(html);
                    var item = $('#editModal').find('div[name="preview"] > span').last();
                    $(item).find('a[name="insert"]').on('click', function () {
                        information.ueEdit.execCommand('inserthtml', '<img src="' + ncGlobal.fileRoot + data.result.data + '">');
                    });
                    $(item).find('a[name="delete"]').on('click', function () {
                        $(this).parents('span').remove();
                    });
                    $('.information-pic img').jqthumb({
                        width: 100,
                        height: 100,
                        after: function (imgObj) {
                            imgObj.css('opacity', 0).animate({opacity: 1}, 2000);
                        }
                    });
                    _refreshImage();
                } else {
                    $.ncAlert({
                        closeButtonText: "关闭",
                        autoCloseTime: 3,
                        content: data.result.message
                    })
                }
            }
        });
    }

    //外部可调用
    return {
        init: function () {
            _bindEvent();
        },
        ueAdd: _ueAdd,
        ueEdit: _ueEdit,
        ueLook: _ueLook,
        delInfo: _delInfo,
        backInfo: _backInfo,
        pubInfo: _pubInfo,

    }
}();




$(function () {
    grid.load();
    grid1.load();
    information.init();
    //实例化编辑器
    information.ueAdd = UE.getEditor('contnetAdd', {textarea: 'content'});
    information.ueEdit = UE.getEditor('contnetEditor', {textarea: 'content'});
    information.ueLook = UE.getEditor('contnetLook', {textarea: 'content'});
})